package yanyi.community.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import yanyi.community.model.User;
import yanyi.community.service.NotificationService;
import yanyi.community.service.QuestionService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class ProfileController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private NotificationService notificationService;

    @GetMapping("profile/{action}")
    public String questions(@PathVariable("action") String action, @RequestParam(value = "pageNum", defaultValue = "1", required = false) int pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "3", required = false) int pageSize,
                            Model model,
                            HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/";
        }
        if ("questions".equals(action)) {
            model.addAttribute("section", "questions");
            model.addAttribute("sectionName", "我的提问");
            Map<String ,Object> map=new HashMap<>();
            map.put("userId",user.getId());
            PageInfo pageInfo = questionService.findAll(map,pageNum, pageSize);
            model.addAttribute("pageInfo", pageInfo);
        }else if("replies".equals(action)) {
            PageInfo pageInfo=notificationService.list(user.getId(),pageNum,pageSize);
            model.addAttribute("section","replies");
            model.addAttribute("sectionName","最新回复");
            model.addAttribute("pageInfo",pageInfo);
        }
        return "profile";
    }
}
